import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import { Message } from 'element-ui'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/',
    name: 'Home',
    redirect: '/welcome',
    component: Home,
    children: [
      {
        path: '/welcome',
        component: () => import('@/views/welcome/')
      },
      {
        path: '/shopInfo',
        component: () => import('@/views/my-shop/shop-info.vue')
      },
      {
        path: '/shopStyle',
        component: () => import('@/views/my-shop/shop-info.vue')
      },
      {
        path: '/shopRights',
        component: () => import('@/views/my-shop/shop-info.vue')
      },
      {
        path: '/shopAccount',
        component: () => import('@/views/finance/shopAccount.vue')
      },
      {
        path: '/shopDetails',
        component: () => import('@/views/finance/shopDetails.vue')
      },
      {
        path: '/goodsList',
        component: () => import('@/views/goods/goods-list')
      },
      {
        path: '/goodsCard',
        component: () => import('@/views/goods/goods-card')
      },
      {
        path: '/orderList',
        component: () => import('@/views/order/index.vue')
      },
      {
        path: '/orderDetail/:id',
        component: () => import('@/views/order/order-detail.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  if (to.path !== '/login' && !window.sessionStorage.getItem('LOGIN_TOKEN')) {
    Message.error('请先登录！')
    return next('/login')
  }
  next()
})
export default router
